#!/usr/bin/env bash

###############################################################################
# Copyright (c) 2021 congzhang Technologies. All rights reserved.
###############################################################################

# Script to automate docker image building and pushing

ROOT_DIR=$(realpath "$(dirname "${BASH_SOURCE[0]}")"/..)
REGISTRY="hub.congzhang.tech"

function info() {
  echo -e "\033[034mInfo:\033[0m $*"
}

function warn() {
  >&2 echo -e "\033[093mWarning: $*\033[0m"
}

function error() {
  >&2 echo -e "\033[091mERROR: $*\033[0m"
}

function build() {
  info "download third-party..."
  docker build  -t "congzhang/builder:latest" "$ROOT_DIR/docker"    # net
  # docker build  -t "congzhang/builder:latest" "$ROOT_DIR/docker" -f "$ROOT_DIR/docker/Dockerfile.locally" #local
  rm "$ROOT_DIR/docker/local_dep" -rf
}

function push() {
  TAG=$(date +%Y%m%d_%H%M)
  docker tag "congzhang/builder:latest" \
    "$REGISTRY/internal/safety-island/builder:$TAG"
  docker push "$REGISTRY/internal/safety-island/builder:$TAG"
  info "Successfully pushed image"
  echo "internal/safety-island/builder:$TAG" > \
    "$ROOT_DIR/docker_image.tag"
  git add "$ROOT_DIR/docker_image.tag"
}

set -e

build
push
